package com.yingxue.lesson.controller;

import com.yingxue.lesson.entity.SysDept;
import com.yingxue.lesson.exception.DataResult;
import com.yingxue.lesson.service.SysDeptService;
import com.yingxue.lesson.vo.req.DeptAddReqVO;
import com.yingxue.lesson.vo.resp.DeptRespNodeVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (SysDept)表控制层
 *
 * @author makejava
 * @since 2022-04-05 21:40:13
 */
@RestController
@RequestMapping("/api")
public class SysDeptController {
    /**
     * 服务对象
     */
    @Resource
    private SysDeptService sysDeptService;

    @GetMapping("/depts")
    @ApiOperation(value = "查询所有部门数据接口")
    public DataResult<List<SysDept>> getAllDept(){
        DataResult result =DataResult.success();
        List<SysDept> deptList = sysDeptService.selectAll();
        result.setData(deptList);
        return result;
    }

    @GetMapping("/dept/tree")
    @ApiOperation(value = "部门树形结构列表接口")
    public DataResult<List<DeptRespNodeVO>> getDeptTree(@RequestParam(required = false) String deptId){
        DataResult result=DataResult.success();
        List<DeptRespNodeVO> deptRespNodeVOList = sysDeptService.deptTreeList();
        result.setData(deptRespNodeVOList);
        return result;
    }

    @PostMapping("/dept")
    @ApiOperation(value = "新增部门数据接口")
    public DataResult<SysDept> addDept(@RequestBody @Valid DeptAddReqVO vo){
        DataResult result=DataResult.success();
        SysDept sysDept = sysDeptService.addDept(vo);
        result.setData(sysDept);
        return result;
    }


}

